"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.pickSvgs = void 0;
const templates_1 = require("./templates");
const tools_1 = require("./tools");
const fs = require("fs");
const path = require("path");
function pickSvgs(options) {
    options.outputValue = options.outputValue || options.inputValue;
    const { inputValue = "", pick } = options;
    if (!pick || typeof pick !== "string") {
        throw new Error("pick should be a string");
    }
    const inputPath = path.join(process.cwd(), ...inputValue.split("/").filter(Boolean));
    if (!fs.statSync(inputPath).isFile() && !inputValue.endsWith(".js")) {
        throw new Error("input should be a .js file");
    }
    const { svgId, svgList: oldSvgList } = (0, tools_1.getIconfontContent)(inputPath);
    const pickIds = pick.split(",").filter(Boolean);
    outputFile(svgId, pickIds, oldSvgList, options);
}
exports.pickSvgs = pickSvgs;
// 输出iconfont.js文件
function outputFile(svgId, pickIds, svgList, options) {
    const { outputValue } = options;
    const pickMap = pickIds.reduce((p, id) => {
        p[id] = true;
        return p;
    }, {});
    const outputPath = (0, tools_1.getOutputPath)(outputValue, "iconfont.js", ".js");
    // 提取剩余的svg
    const restSvgList = [];
    // 要提取出来的svg
    const pickSvgList = [];
    svgList.forEach((svg) => {
        if (pickMap[svg.name]) {
            pickSvgList.push(svg);
        }
        else {
            restSvgList.push(svg);
        }
    });
    // 提取剩余的svg
    const content = (0, templates_1.generateIconfontContent)(restSvgList.map(templates_1.generateSymbolContnt).join(""), svgId);
    fs.writeFileSync(outputPath, content);
    const html = (0, templates_1.generateHtmlContent)(restSvgList.map((item) => item.name));
    const htmlPath = path.join(path.parse(outputPath).dir, "index.html");
    fs.writeFileSync(htmlPath, html);
    // 被提取的svg文件
    pickSvgList.forEach((svg) => {
        const { content, name, viewBox } = svg;
        const svgPath = path.join(path.parse(outputPath).dir, `${name}.svg`);
        fs.writeFileSync(svgPath, (0, templates_1.generateSvgContent)(content, viewBox));
    });
    const svgHtmlPath = path.join(path.parse(outputPath).dir, "svgIndex.html");
    fs.writeFileSync(svgHtmlPath, (0, templates_1.generateSvgsHtmlContent)(pickSvgList.map((item) => item.name)));
}
